<?php
action_gatekeeper ( 'outregister' );
$project = get_entity ( $vars ['project_guid'] );
if ($project) {
	$members = $project->students;
	$user = elgg_get_logged_in_user_entity ();
	if (is_array ( $members )) {
		foreach ( $members as $member ) {
			if ($user->username == $member) {
				$members = array_diff ( $members, array (
						"$member" 
				) );
				if (strpos ( $user->classes, ',' ) !== false) {
					$user->classes = str_replace ( ",$project->group_project`$project->semester`$project->academic_year", "", $user->classes );
					$user->classes = str_replace ( "$project->group_project`$project->semester`$project->academic_year", "", $user->classes );
				} else {
					$user->classes = null;
				}
				$member_count = 0;
				$user->save ();
				break;
			}
		}
		$member_count = count ( $members );
	} else {
		
		if ($user->username == $members) {
			if (strpos ( $user->classes, ',' ) !== false) {
				$user->classes = str_replace ( ",$project->group_project`$project->semester`$project->academic_year", "", $user->classes );
				$user->classes = str_replace ( "$project->group_project`$project->semester`$project->academic_year", "", $user->classes );
			} else {
				$user->classes = null;
			}
			$member_count = 0;
			$user->save ();
		}
	}
	if ($member_count == 0) {
		$project->delete ( true );
	} else {
		$project->students = $members;
		$project->save ();
	}
	system_message("Rời khỏi danh sách chờ thành công");
	//system_message ( elgg_echo ( 'Rời đăng kí thành công' ) );
}

forward ( REFERER );